package com.ydgg.ltar.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ydgg.ltar.mapper.ArBuildingContentMapper;
import com.ydgg.ltar.mapper.ArBuildingMapper;
import com.ydgg.ltar.model.ArBuilding;
import com.ydgg.ltar.model.ArBuildingContent;
import com.ydgg.ltar.util.PageInfo;
import com.ydgg.ltar.util.Result;


/**
 * AR识楼
 * 作者： 张叶
 * 创建时间：2018年12月5日  下午3:39:27
 */
@Service
public class ArBuildingService {

	@Autowired
	private ArBuildingMapper arBuildingMapper;
	
	@Autowired
	private ArBuildingContentMapper arBuildingContentMapper;


	/**
	 * 案例
	 * 作者： 张叶
	 * 创建时间：2018年12月29日  上午9:46:01
	 */
	public List<Map<String,Object>> selectExample() {
		List<Map<String,Object>> list = arBuildingMapper.selectExample();
		return list;
	}
	
	public List<ArBuilding> selectByDistance(String lat,String lng) {
		List<ArBuilding> list = arBuildingMapper.selectByDistance(lat,lng);
		return list;
	}
	
	public ArBuilding selectByPrimaryKey(Long id){
		ArBuilding ab = arBuildingMapper.selectByPrimaryKey(id);
		return ab;
	}
	
	
	public List<Map<String,Object>> selectByPage(PageInfo pageInfo, String where, String order) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		List<Map<String, Object>> m = arBuildingMapper.selectByPage(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}
	
	
	public List<ArBuilding> selectByWhere(String where) {
		List<ArBuilding> list = arBuildingMapper.selectByWhere(where);
		return list;
	}
	
	
	/**
	 * 添加
	 * 作者： 张叶
	 * 创建时间：2018年12月6日  上午9:49:28
	 */
	@Transactional
	public Result insert(ArBuilding ab){
		Result result = new Result();
		ab.setCreatedAt(new Date());
		ab.setStatus("on");
		int i = arBuildingMapper.insert(ab);
		if(i > 0){
			result.code = 0;
			result.message = "添加成功";
		}else{
			result.code = 1;
			result.message = "系统错误，添加失败";
		}
		return result;
	}

	
	@Transactional
	public int updateBySelective(ArBuilding ab){
		int i = arBuildingMapper.updateBySelective(ab);
		return i;
	}
	

	/**
	 * 删除
	 * 作者： 张叶
	 * 创建时间：2018年12月6日  下午5:58:19
	 */
	@Transactional
	public Result deleted(ArBuilding ab){
		Result result = new Result();
		String where = " and ar_building_id = " + ab.getId();
		List<ArBuildingContent> list = arBuildingContentMapper.selectByWhere(where);
		if(list.size() == 0){
			ab.setStatus("deleted");
			int i = arBuildingMapper.updateBySelective(ab);
			if(i > 0){
				result.code = 0;
				result.message = "删除成功";
			}else{
				result.code = 1;
				result.message = "系统错误，删除失败";
			}
		}else{
			result.code = 2;
			result.message = "有级联内容，删除失败";
		}
		return result;
	}
}
